package com.shycloud.mido.member.handler;

import com.shycloud.mido.member.api.dto.MemberLoginInfo;
import com.shycloud.mido.member.api.entity.Member;
import com.shycloud.mido.member.api.vo.MemberDetailForLoginVO;

/**
 * @author npc
 * @date 2018/11/18
 */
public abstract class AbstractLoginHandler implements LoginHandler {

	/***
	 * 数据合法性校验
	 * @param loginStr 通过用户传入获取唯一标识
	 * @return 默认不校验
	 */
	@Override
	public Boolean check(String loginStr) {
		return true;
	}

	/**
	 * 处理方法
	 *
	 * @param loginStr 登录参数
	 * @return
	 */
	@Override
	public MemberLoginInfo handle(String loginStr) {
		MemberLoginInfo memberLoginInfo = new MemberLoginInfo();

		if (!check(loginStr)) {
			memberLoginInfo.setStatus("-2");
			return memberLoginInfo;
		}

		String identify = identify(loginStr);

		if (identify == null) {
			memberLoginInfo.setStatus("-1");
			return memberLoginInfo;
		}

		MemberDetailForLoginVO member = info(identify);

		if (member == null) {
			memberLoginInfo.setOpenId("账号不存在");
			memberLoginInfo.setStatus("0");
			return memberLoginInfo;
		}

		if ("3".equals(member.getStatus())) {
			memberLoginInfo.setOpenId(member.getWxOpenid());
			memberLoginInfo.setStatus("3");
			return memberLoginInfo;
		}

		memberLoginInfo.setMember(member);
		memberLoginInfo.setOpenId(identify);
		memberLoginInfo.setStatus("1");
		return memberLoginInfo;
	}
}
